<template>
    <div class="project-setting">
        <div class="pro-info">
            <div class="author-photo">
                <img :src="teamAvatarUrl || defaultTeamAvatar" />
                <div class="overlay">visit</div>
            </div>
            <div class="pro-info-msg">
                {{ props?.teamName || 'unknown' }}
            </div>

            <div class="pro-info-msg">
                {{ props?.authorName ?? 'unknown' }}
            </div>
        </div>
        <hr class="vertical-hr" />
        <div class="setting-left">
            <div class="img-view">
                <div class="info">Cover</div>
                <div class="img-container" @click="triggerFile('cover')">
                    <input type="file" accept="image/*" ref="coverInput" @change="handleFileChange">
                    <img :src="coverUrl??defaultCoverUrl" />
                    <div class="overlay">Click to upload</div>
                </div>
            </div>
            <div class="description">
                <div class="d-l">
                    description
                </div>
                <div class="d-r">
                    <textarea v-model="description" placeholder="a simple project"></textarea>
                </div>
            </div>
            <div class="is-public">
                <div class="d-l">
                    public
                </div>
                <div class="d-r">
                    <input type="checkbox" v-model="is_Public" />
                </div>
            </div>
            <div class="slink" style="width:400px;height:30px; text-align:center;" @click="savePjEdit">
                Save Settings
            </div>
        </div>
        <div class="setting-right">
            <div class="img-view">
                <div class="info">
                    Thumbnail
                </div>
                <div class="img-container" @click="setCover(thumbnailUrl)">
                    <img :src="thumbnailUrl || defaultThumbnailUrl" />
                    <div class="overlay">Set as cover</div>
                </div>
            </div>
            <div class="img-view">
                <div class="info">
                    LastView
                </div>
                <div class="img-container" @click="setCover(thumbnailUrl1)">
                    <img :src="thumbnailUrl1 || defaultThumbnailUrl" />
                    <div class="overlay">Set as cover</div>
                </div>
            </div>
            <div class="img-view">
                <div class="info">
                    SaveView
                </div>
                <div class="img-container" @click="setCover(saveViewUrl)">
                    <img :src="saveViewUrl || defaultThumbnailUrl" />
                    <div class="overlay">Set as cover</div>
                </div>
            </div>
        </div>
    </div>
</template>

<script lang="ts" setup>
import { nextTick, ref, onMounted } from 'vue'
import proCover1 from '@/assets/images/pro_cover1.png';
import thumbnail1 from '@/assets/images/thumbnail1.png';
import defaultAvatar from '@/assets/images/default_profile.png';
import defaultTeamAvatar from '@/assets/images/default_teamimage.png';
const props = withDefaults(
    defineProps<{
        projectID: string

        authorName?: string
        authorID?: string
        teamName?: string
        teamID?: string
        teamAvatarUrl?: string

        isPublic?: boolean
        description?: string

        thumbnailUrl?: any
        thumbnailUrl1?: any
        saveViewUrl?: any
    }>(),
    {
        description: 'a simple project',
        thumbnailUrl: thumbnail1,
        thumbnailUrl1: thumbnail1,
        saveViewUrl: thumbnail1,
        teamAvatarUrl: defaultTeamAvatar,
        isPublic: false,
        authorName: 'unknown',
        authorID: 'unknown',
        teamName: 'unknown',
        teamID: 'unknown',
    }
)
const thumbnailUrl = ref(thumbnail1)
const thumbnailUrl1 = ref(thumbnail1)
const coverUrl = ref(proCover1)
const saveViewUrl = ref(thumbnail1)
const defaultThumbnailUrl = ref(thumbnail1)
const defaultCoverUrl = ref(proCover1)


const coverInput = ref<HTMLInputElement | null>(null);
const thumbInput = ref<HTMLInputElement | null>(null);
const fileClick = ref('')
const description = ref('')
onMounted(() => {
    description.value = props.description
    thumbnailUrl.value = props.thumbnailUrl
    thumbnailUrl1.value = props.thumbnailUrl1
    saveViewUrl.value = props.saveViewUrl
})
const triggerFile = (type: 'cover' | 'thumbnail') => {
    fileClick.value = type
    if (type === 'cover') coverInput.value?.click();
    else thumbInput.value?.click();
};

const handleFileChange = (event: Event) => {
    let type = fileClick.value
    const target = event.target as HTMLInputElement;
    const file = target.files?.[0];

    if (!file) return;
    const reader = new FileReader();
    reader.onload = async (e) => {
        const result = e.target?.result as string;
        if (type === 'cover') coverUrl.value = result;
        else thumbnailUrl.value = result;
        //console.log('读取完毕', result)
    };
    reader.readAsDataURL(file);
    target.value = '';
    fileClick.value = ''
};
const setCover = (url: string) => {
    coverUrl.value = url
}
const is_Public = ref(props.isPublic)
const visitProfile = (id: string) => {
    //window.open(`/profile/${id}`)
}
import {useServer} from '@/components/ts/useServer';
import {useUserStore} from '@/stores/userStore';
import {useProjectsStore} from '@/stores/projectsStore';
const projectsStore = useProjectsStore();
const userStore = useUserStore();

import { message } from 'ant-design-vue';

const savePjEdit = async () => {
    let pj = await projectsStore.updatepjinfo(
        props.projectID,
        {
            is_opened: true,
            description: description.value,
            //thumbnail: coverUrl.value,
            isPublic: is_Public.value,
        }
    )
    if (pj) {
        //pj.thumbnail = coverUrl.value
        pj.isPublic = is_Public.value
        pj.description = description.value
console.log(projectsStore.projects)
        message.success('Edit Success')
    }
    else {
        console.log('Edit Failed')
    }
}
</script>
<style scoped lang="scss">
.project-setting {
    width: 100%;
    height: 100%;
    justify-content: center;
    display: flex;
    overflow-x: hidden;
    overflow-y: auto;
    position: relative;
    padding-top:5px;

}

.pro-info {
    width: 100px;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 3px;

    .pro-info-msg {
        width: 80px;
        height: 30px;
        display: flex;
        font-size: 14px;
        font-weight: bold;
        justify-content: center;
        align-content: center;
    }

    .author-photo {
        width: 48px;
        height: 48px;
        position: relative;
        border-radius: 100%;
        cursor: pointer;
        overflow: hidden;
        user-select: none;

        img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }
    }
}

.setting-left {
    flex: 1;
    height: 100%;
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;

    .description {
        width: 400px;
        height: 150px;
        display: flex;

    }

    .is-public {
        width: 400px;
        height: 50px;
        display: flex;
    }

    .d-l {
        width: 100px;
        height: 100%;
        font-weight: bold;
    }

    .d-r {
        flex: 1;
        height: 100%;

        textarea {
            width: 100%;
            height: 100%;
            resize: none;
            overflow: hidden;
            font-size: 16px;
            font-family: Arial, Helvetica, sans-serif;
            font-weight: bold;
            border: none;
            outline: none;
            border-radius: 6px;
            transition: box-shadow 0.3s ease;

            &:focus {
                box-shadow: 0 0 0 1px #409eff;
                /* 用阴影来当边框 */
            }
        }

        input[type="checkbox"] {
            appearance: none;
            /* 去掉浏览器默认样式 */
            -webkit-appearance: none;
            -moz-appearance: none;

            width: 20px;
            height: 20px;
            border: 2px solid #409eff;
            border-radius: 4px;
            cursor: pointer;
            display: inline-block;
            position: relative;
            transition: all 0.2s ease;

            &:checked {
                background-color: #ffbbbb;
                border-color: #409eff;
            }

            &:checked::after {
                content: "✔";
                color: #389bfe;
                font-size: 14px;
                position: absolute;
                top: 50%;
                left: 50%;
                transform: translate(-50%, -55%);
            }
        }
    }
}

.setting-right {
    width: 500px;
    height: 100%;
    position: relative;
    display: flex;
    align-content: center;
    flex-direction: column;
    gap: 20px;
    //overflow-y: auto;
}

.img-view {
    width: 400px;
    height: 200px;
    position: relative;
    display: flex;

    .info {
        width: 100px;
        font-weight: bold;
        height: 100%;
    }

    .img-container {
        position: relative;
        overflow: hidden;
        flex: 1;
        height: 100%;
        border: 2px dashed #ccc;
        border-radius: 12px;
        cursor: pointer;
        overflow: hidden;
        transition: border-color 0.3s;

        &:hover {
            border-color: #409eff;
        }

        img {
            width: 100%;
            height: 100%;
            object-fit: fill; //拉伸填充
            border-radius: 12px;
        }

        input {
            display: none;
        }

    }
}

.slink {
    font-size: 14px;
    font-weight: bold;
    border: 2px dashed #409eff;//虚线
    border-radius: 12px;
    cursor: pointer;
    transition: color 0.3s ease, transform 0.3s ease;

    &:hover {
        color: #66b1ff;
    }
}
.overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #fff;
    background: rgba(0, 0, 0, 0.3);
    font-size: 16px;
    font-weight: bold;
    opacity: 0;
    transition: opacity 0.3s;
    border-radius: 12px;

    &:hover {
        opacity: 1;
    }
}

.rdbutton {
    position: absolute;
    right: 5px;
    bottom: 3px;
    width: 180px;
    height: 30px;
    font-family: 'Courier New', Courier, monospace;
    font-weight: bold;
}

.vertical-hr {
    width: 2px;
    border: none;
    border-left: 2px dashed #409eff;
    transform: rotate(0deg);
    height: 100%;
}
</style>
